import { type ConstantItemQueryParams, type ConstantItemInfo, type ConstantItemSave, type EnumConstantInfo, type EnumConstantQueryParams, type EnumConstantSave } from "@/types";
import axios from "axios";
import { ElMessage } from "element-plus";
import { id } from "element-plus/es/locales.mjs";
import { reactive, ref } from "vue";

export default function() {
    let enumConstantInsertWindowsEnable = ref(false);
    let enumConstantItemWindowsEnable = ref(false);
    let insertConstantItemWindowEnable = ref(false);
    let enumConstantUpdateWindowsEnable = ref(false);
    let updateConstantItemWindowEnable = ref(false);
    let constantItemcurrentPage = ref(1);
    let constantItemPageSize = ref(10);
    let enumConstantCurrentPage = ref(1);
    let enumConstantPageSize = ref(10);
    let enumConstantQueryParams = reactive<EnumConstantQueryParams>({
        constantKey: '',
        constantName: '',
        isEnable: 0
    })
    let constantItemQueryParams = reactive<ConstantItemQueryParams>({
        constantKey: '',
        itemKey: '',
        itemName: ''
    })
    let enumConstantWindow = reactive({
        constantKey: '',
        constantName: '',
        description: '',
        itemType: '',
        isEnable: ''
    })
    let enumConstantSave = reactive<EnumConstantSave>({
        id: '',
        constantKey: '',
        constantName: '',
        description: '',
        itemType: 'string',
        isEnable: 0
    })
    let enumConstantupdate = reactive<EnumConstantSave>({
        id: '',
        constantKey: '',
        constantName: '',
        description: '',
        itemType: 'string',
        isEnable: 0
    })
    let constantItemSave = reactive<ConstantItemSave>({
        id: '',
        constantKey: '',
        itemKey: '',
        description: '',
        itemName: '',
    })
    let constantItemUpdate = reactive<ConstantItemSave>({
        id: '',
        constantKey: '',
        itemKey: '',
        description: '',
        itemName: '',
    })
    let constantItemTypeOptions = [
        {
            value: 'string',
            label: '文本'
        },
        {
            value: 'double',
            label: '数字'
        }
    ]
    let enumConstantInfoList = reactive<EnumConstantInfo[]>([])
    let constantItemInfoList = reactive<ConstantItemInfo[]>([])
    async function getEnumConstantListByQueryParams(params:EnumConstantQueryParams) {
        console.log("调用后端接口查询枚举常量")
        let result = await axios.post("/enumConstant/select/byQuery", params)
        console.log(result.data)
        return result.data
    }
    async function getConstantItemListByQueryParams(params:ConstantItemQueryParams) {
        let result = await axios.post("/constantItem/select/byQuery", params)
        console.log(result.data)
        return result.data
    }
    function openInsertEnumConstantWindow() {
        console.log("调用后端接口新增枚举常量")
        enumConstantInsertWindowsEnable.value = true
    }
    function openupdateEnumConstantWindow() {
        console.log("调用后端接口新增枚举常量")
        enumConstantUpdateWindowsEnable.value = true
    }
    async function updateEnumConstantOnSubmit(params: EnumConstantSave) {
        console.log('update submit!')
        console.log(JSON.stringify(params))
        let result = await axios.post("/enumConstant/update", params).then(res => {
            console.log(res)
            ElMessage({
                type: 'success',
                message: '修改枚举常量成功!'
            });
            return res
        })
        console.log(result.data)
        // 清空参数
        Object.assign(enumConstantupdate, {
            id: 0,
            constantKey: '',
            constantName: '',
            description: '',
            itemType: '',
            isEnable: 1
        })
        enumConstantUpdateWindowsEnable.value = false
        updateEnumConstantList()
        return result.data
    }
    async function deleteEnumConstant(id: number) {
        console.log('删除枚举常量')
        console.log(id)
        let result = await axios.get("/enumConstant/delete/" + id).then(res => {
            console.log(res)
            if (res.data.code == 200) {
                ElMessage({
                    type: 'success',
                    message: '删除枚举常量成功!'
                });
            } else {
                ElMessage({
                    type: 'error',
                    message: res.data.msg
               });
            }
        })
        // 更新列表
        updateEnumConstantList()
    }
    function updateEnumConstantList() {
        getEnumConstantListByQueryParams(enumConstantQueryParams).then(res => {
            console.log('删除信息后，更新列表')
            if (Array.isArray(res.content)) {
                enumConstantInfoList.length = 0;
                Object.assign(enumConstantInfoList, res.content)
            } else {
                console.log('查询结果为空')
            }
        })
    }
    async function insertEnumConstantOnSubmit(enumConstantSave: EnumConstantSave) {
        console.log('submit!')
        console.log(JSON.stringify(enumConstantSave))
        let result = await axios.post("/enumConstant/insert", enumConstantSave).then(res => {
            console.log(res)
            if (res.data.code == 200) {
                ElMessage({
                    type: 'success',
                    message: '新增枚举常量成功!'
                });
            } else {
                ElMessage({
                    type: 'error',
                    message: res.data.msg
                });

            }
            return res
        })
        console.log(result.data)
        // 清空参数
        Object.assign(enumConstantSave, {
            id: 0,
            constantKey: '',
            constantName: '',
            description: '',
            itemType: '',
            isEnable: 0
        })
        enumConstantInsertWindowsEnable.value = false
        updateEnumConstantList()
        return result.data
    }
    async function insertConstantItemOnSubmit(params: ConstantItemSave) {
        console.log('保存常量明细项')
        params.constantKey = enumConstantWindow.constantKey
        console.log(JSON.stringify(params))
        let result = await axios.post("/constantItem/insert", params).then(res => {
            console.log('新增结果')
            console.log(res)
            enumConstantInsertWindowsEnable.value = false
            // 弹框(成功弹框、失败弹框)
            ElMessage({
                type: res.data.code === 200 ? 'success' : 'error',
                message: res.data.code === 200 ? '新增成功' : '新增失败'
            })
        })
        insertConstantItemWindowEnable.value = false
        // 清空参数
        constantItemSave.itemKey = ''
        constantItemSave.itemName = ''
        constantItemSave.description = ''
        // 更新列表
        updateConstantItemList()
    }
    function updateConstantItemList() {
        getConstantItemListByQueryParams(constantItemQueryParams).then(res => {
            console.log('删除信息后，更新列表')
            if (Array.isArray(res.content)) {
                constantItemInfoList.length = 0;
                Object.assign(constantItemInfoList, res.content)
            } else {
                console.log('查询结果为空')
            }
        })

    }
    function getConstantItemTypeLabel(itemType: string): string {
        const option = constantItemTypeOptions.find(option => option.value === String(itemType));
        return option ? option.label : '';
    }
    async function axiosUpdateEnumConstant(params:EnumConstantSave) {
        console.log("调用后端接口更新枚举常量")
        let result = await axios.post("/enumConstant/update", params)
        console.log(result.data)
        return result.data
    }
    async function deleteConstantItem(id: number) {
        console.log('删除常量明细项')
        console.log(id)
        let resutl = await axios.get("/constantItem/delete/" + id).then(res => {
            console.log(res)
            if (res.data.code == 200) {
                ElMessage({
                    type: 'success',
                    message: '删除常量明细项成功!'
                });
            } else {
                ElMessage({
                    type: 'error',
                    message: res.data.msg
                })
            }
        })
        updateConstantItemList()
    }
    function openUpdateConstantItemWindow() {
        updateConstantItemWindowEnable.value = true
    }
    async function updateConstantItemOnSubmit(params: ConstantItemSave) {
        console.log('修改常量明细项')
        console.log(JSON.stringify(params))
        let result = await axios.post("/constantItem/update", params).then(res => {
            console.log(res)
            updateConstantItemWindowEnable.value = false
            // 弹框(成功弹框、失败弹框)
            ElMessage({
                type: res.data.code === 200 ? 'success' : 'error',
                message: res.data.code === 200 ? '修改成功' : '修改失败'
            })
        })
        // 更新列表
        updateConstantItemList()
    }
    // 明细项分页处理页码
    function constantItemHandleCurrentChange(param: number) {
        constantItemcurrentPage.value = param
    }
    // 明细项处理每页条数变化
    function constantItemHandleSizeChange(param: number) {
        constantItemPageSize.value = param
        constantItemcurrentPage.value = 1
    }
    // 常量分页处理页码
    function enumConstantHandleCurrentChange(param: number) {
        enumConstantCurrentPage.value = param
    }
    // 常量处理每页条数变化
    function enumConstantHandleSizeChange(param: number) {
        enumConstantPageSize.value = param
        enumConstantCurrentPage.value = 1
    }
    return {
        enumConstantInsertWindowsEnable,
        enumConstantUpdateWindowsEnable,
        enumConstantItemWindowsEnable,
        insertConstantItemWindowEnable,
        updateConstantItemWindowEnable,
        enumConstantQueryParams,
        constantItemQueryParams,
        enumConstantWindow,
        enumConstantSave,
        enumConstantupdate,
        constantItemSave,
        constantItemUpdate,
        enumConstantInfoList,
        constantItemInfoList,
        constantItemTypeOptions,
        constantItemcurrentPage,
        constantItemPageSize,
        enumConstantCurrentPage,
        enumConstantPageSize,
        getEnumConstantListByQueryParams,
        getConstantItemListByQueryParams,
        openInsertEnumConstantWindow,
        openupdateEnumConstantWindow,
        openUpdateConstantItemWindow,
        insertEnumConstantOnSubmit,
        updateEnumConstantOnSubmit,
        deleteEnumConstant,
        axiosUpdateEnumConstant,
        insertConstantItemOnSubmit,
        deleteConstantItem,
        updateConstantItemOnSubmit,
        getConstantItemTypeLabel,
        constantItemHandleCurrentChange,
        constantItemHandleSizeChange,
        enumConstantHandleSizeChange,
        enumConstantHandleCurrentChange
    }
}